#!/usr/bin/perl

# Try wallpaper --schemedata:0.1 | awk 'BEGIN { FS = ":" } ; { print $2 }' | xargs schemer

use Color::Scheme;
use Image::Magick;

my $basecolor = $ARGV[0];
my ( %colorscheme );

my $currentscheme = makescheme( $basecolor );
hashcolors( $currentscheme->[0], 'primary' );
hashcolors( $currentscheme->[1], 'warm' );
hashcolors( $currentscheme->[2], 'cool' );
hashcolors( $currentscheme->[3], 'comp' );

my $currentscheme = makescheme( $colorscheme{ 'warm' }{ 'base' } );
hashcolors( $currentscheme->[1], 'warmer' );
hashcolors( $currentscheme->[3], 'comp_warm' );

my $currentscheme = makescheme( $colorscheme{ 'warmer' }{ 'base' } );
hashcolors( $currentscheme->[1], 'warmest' );
hashcolors( $currentscheme->[3], 'comp_warmer' );

my $currentscheme = makescheme( $colorscheme{ 'cool' }{ 'base' } );
hashcolors( $currentscheme->[2], 'cooler' );
hashcolors( $currentscheme->[3], 'comp_cool' );

my $currentscheme = makescheme( $colorscheme{ 'cooler' }{ 'base' } );
hashcolors( $currentscheme->[2], 'coolest' );
hashcolors( $currentscheme->[3], 'comp_cooler' );

my $image = Image::Magick->new;

$image->Set( size => '600x610' );
$image->ReadImage( "xc:white" );

drawscheme( '1',   '1',   'primary' );
drawscheme( '150', '1',   'warm' );
drawscheme( '300', '1',   'warmer' );
drawscheme( '450', '1',   'warmest' );
drawscheme( '1',   '150', 'cool' );
drawscheme( '1',   '300', 'cooler' );
drawscheme( '1',   '450', 'coolest' );

drawscheme( '450', '450', 'comp' );
drawscheme( '450', '300', 'comp_warm' );
drawscheme( '450', '150', 'comp_warmer' );

drawscheme( '300', '450', 'comp_cool' );
drawscheme( '150', '450', 'comp_cooler' );

$image->Display;

sub drawscheme {
    my ( $x, $y, $scheme ) = @_;

    my $x_10 = $x + 10;

    my $x_20 = $x + 20;
    my $y_20 = $y + 20;

    my $x_30 = $x + 30;
    my $y_30 = $y + 30;

    my $x_70 = $x + 70;
    my $y_70 = $y + 70;

    my $x_80 = $x + 80;
    my $y_80 = $y + 80;

    my $x_90 = $x + 90;
    my $y_90 = $y + 90;

    my $x_130 = $x + 130;
    my $y_130 = $y + 130;

    my $x_140 = $x + 140;
    my $y_140 = $y + 140;

    my $x_150 = $y + 150;
    my $y_150 = $y + 150;

    $image->Draw( primitive => 'rectangle',
                  fill      => "#" . $colorscheme{ $scheme }{ 'base' },
                  stroke    => "#" . $colorscheme{ $scheme }{ 'base' },
                  points    => $x_10 . "," . $y_20 . " " . $x_140 . "," . $y_150
                );
    $image->Draw( primitive => 'rectangle',
                  fill      => "#" . $colorscheme{ $scheme }{ 'base' },
                  stroke    => "#" . $colorscheme{ $scheme }{ 'dark' },
                  points    => $x_20 . "," . $y_30 . " " . $x_70 . "," . $y_80
                );

    $image->Draw( primitive => 'rectangle',
                  fill      => "#" . $colorscheme{ $scheme }{ 'dark' },
                  stroke    => "#" . $colorscheme{ $scheme }{ 'dark' },
                  points    => $x_80 . "," . $y_30 . " " . $x_130 . "," . $y_80
                );

    $image->Draw( primitive => 'rectangle',
                  fill      => "#" . $colorscheme{ $scheme }{ 'pale' },
                  stroke    => "#" . $colorscheme{ $scheme }{ 'pale' },
                  points    => $x_20 . "," . $y_90 . " " . $x_70 . "," . $y_140
                );

    $image->Draw( primitive => 'rectangle',
                  fill      => "#" . $colorscheme{ $scheme }{ 'lpale' },
                  stroke    => "#" . $colorscheme{ $scheme }{ 'lpale' },
                  points    => $x_80 . "," . $y_90 . " " . $x_130 . "," . $y_140
                );

    $image->Annotate( pointsize => 10,
                      fill      => "#" . $colorscheme{ $scheme }{ 'dark' },
                      x         => $x + 20,
                      y         => $y + 15,
                      text      => "$scheme"
                    );

    $image->Annotate( pointsize => 10,
                      fill      => "#" . $colorscheme{ $scheme }{ 'pale' },
                      x         => $x + 35,
                      y         => $y + 57,
                      text      => "base"
                    );

    $image->Annotate( pointsize => 10,
                      fill      => "#" . $colorscheme{ $scheme }{ 'pale' },
                      x         => $x + 95,
                      y         => $y + 57,
                      text      => "dark"
                    );

    $image->Annotate( pointsize => 10,
                      fill      => "#" . $colorscheme{ $scheme }{ 'dark' },
                      x         => $x + 35,
                      y         => $y + 117,
                      text      => "pale"
                    );

    $image->Annotate( pointsize => 10,
                      fill      => "#" . $colorscheme{ $scheme }{ 'dark' },
                      x         => $x + 95,
                      y         => $y + 117,
                      text      => "lpale"
                    );

} ## end sub drawscheme

sub makescheme {
    my ( $initcolor ) = @_;
    my $scheme = Color::Scheme->new;
    $scheme->from_hex( $initcolor );
    $scheme->scheme( 'analogic' );
    $scheme->add_complement( 1 );
    $scheme->distance( 0.4 );    # .25 = 15 degrees and .50 = 30 degrees on the colorwheel
  return $scheme->colorset;
} ## end sub makescheme

sub hashcolors {
    my ( $array, $name ) = @_;

    $colorscheme{ $name }{ 'base' }  = $array->[0];
    $colorscheme{ $name }{ 'dark' }  = $array->[1];
    $colorscheme{ $name }{ 'pale' }  = $array->[2];
    $colorscheme{ $name }{ 'lpale' } = $array->[3];

} ## end sub hashcolors

package Color::Scheme;

sub DESTROY {
    my $self = shift;
    undef $self;
}
